﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Cofe.Core.Property;
using Cofe.Core.Utils;

namespace Cofe.Core.Dynamic
{
    public static class IDynamicPropertyInterfaceProviderExtensions
    {
        public static bool CanCastAs(this IDynamicPropertyInterfaceProvider provider, IPropertyHost propertyHost, Type type)
        {
            Type providerType = typeof(IDynamicInterfaceProvider<IPropertyHost>);
            MethodInfo canCastAs = providerType.GetMethod("CanCastAs").MakeGenericMethod(type);
            return (bool)canCastAs.Invoke(provider, new object[] { propertyHost });
        }

        public static object CastAs(this IDynamicPropertyInterfaceProvider provider, IPropertyHost propertyHost, Type type)
        {
            Type providerType = typeof(IDynamicInterfaceProvider<IPropertyHost>);
            MethodInfo castAs = providerType.GetMethod("CastAs").MakeGenericMethod(type);
            return castAs.Invoke(provider, new object[] { propertyHost });
        }

        public static bool CanCastAs(this IDynamicPropertyInterfaceManager manager, IPropertyHost propertyHost, Type type)
        {
            Type providerType = typeof(IDynamicPropertyInterfaceManager);
            MethodInfo canCastAs = providerType.GetMethod("CanCastAs").MakeGenericMethod(type);
            return (bool)canCastAs.Invoke(manager, new object[] { propertyHost });
        }

        public static object CastAs(this IDynamicPropertyInterfaceManager manager, IPropertyHost propertyHost, Type type)
        {
            Type providerType = typeof(IDynamicPropertyInterfaceManager);
            MethodInfo castAs = providerType.GetMethod("CastAs").MakeGenericMethod(type);
            return castAs.Invoke(manager, new object[] { propertyHost });
        }              
    }
}
